﻿/**************************************************************************
创建时间:	2020/5/15
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：GridComBox 自动初始化的实现

记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.Utils.Helpers;
using ZhCun.Win.Controls;
using ZhCun.Win.Extend;

namespace ZhCun.Win.ExtendGridCombox
{
    public static class FormGridComboxEx
    {
        static readonly GridComboxBLL BLLObj = new GridComboxBLL();

        /// <summary>
        /// 通过数据库配置来初始化CoboxGrid
        /// </summary>
        public static ComboxGridArgs[] InitCoBoxGrid(this IFormContainer frm, ComboxGrid coGrid, params ArgInitCoBoxGrid[] args)
        {
            string formName = frm.ContainerName;
            return InitCoBoxGrid(formName, coGrid, args);
        }
        /// <summary>
        /// 通过数据库配置来初始化CoboxGrid
        /// </summary>
        public static ComboxGridArgs[] InitCoBoxGrid(string formName, ComboxGrid coGrid, params ArgInitCoBoxGrid[] args)
        {
            var ret = BLLObj.GetConfig(formName);
            if (!ret) return null;
            List<ComboxGridArgs> rArgs = new List<ComboxGridArgs>();
            var mainList = ret.data.MainConfig;
            var colList = ret.data.ColumnConfig;
            //if (mainList?.Count == 0) return;
            foreach (var item in args)
            {
                var mainConfig = mainList.Find(s => s.TextBoxName == item.Txt.Name);
                if (mainConfig == null || colList == null) continue;
                var colConfig = colList.FindAll(s => s.MainId == mainConfig.Id);
                if (colConfig == null || colConfig.Count == 0) continue;
                EnumHelper.StringToEnum(mainConfig.AutoSizeColumnsMode, out DataGridViewAutoSizeColumnsMode gridAutoSizeMode);
                ComboxGridArgs coArg = new ComboxGridArgs
                {
                    Columns = new ComboxGridColumn[colConfig.Count],
                    GetDataSourceHandle = item.GetDataSource,
                    IsAnyKeyShow = mainConfig.IsAnyKeyShow,
                    IsAutoSelected = mainConfig.IsAutoSelected,
                    IsSendTabKey = mainConfig.IsSendTabKey,
                    KeyPressMinlength = mainConfig.KeyPressMinlength,
                    MaxRowCount = mainConfig.MaxRowCount,
                    RetrunTagField = mainConfig.RetrunTagField,
                    ReturnTextField = mainConfig.ReturnTextField,
                    AutoSizeColumnsMode = gridAutoSizeMode,
                    TxtBox = item.Txt
                };
                //DataGridViewAutoSizeColumnsMode
                //DataGridViewAutoSizeColumnMode
                int i = 0;
                foreach (var colItem in colConfig)
                {
                    EnumHelper.StringToEnum(colItem.Alignment, out DataGridViewContentAlignment alignment);
                    EnumHelper.StringToEnum(colItem.ColumnType, out DgvColumnType colType);
                    EnumHelper.StringToEnum(colItem.AutoSizeMode, out DataGridViewAutoSizeColumnMode colAutoSizeMode);
                    coArg.Columns[i++] = new ComboxGridColumn
                    {
                        ColumnCaption = colItem.ColumnCaption,
                        ColumnFiled = colItem.FiledName,
                        ColumnFormat = colItem.FormatStr,
                        ColumnWidth = colItem.ColumnWidth,
                        IsVisible = colItem.IsVisible,
                        Alignment = alignment,
                        AutoSizeMode = colAutoSizeMode,
                        ColumnType = colType
                    };
                }
                coGrid.InitArgs(coArg);
                rArgs.Add(coArg);
            }

            return rArgs.ToArray();
        }
    }

    /// <summary>
    /// 初始化CoBoxGrid 参数
    /// </summary>
    public class ArgInitCoBoxGrid
    {
        /// <summary>
        /// 文本框对象
        /// </summary>
        public EditText Txt { set; get; }
        /// <summary>
        /// 获取数据源委托
        /// </summary>
        public Func<string, object> GetDataSource { set; get; }
    }
}